package com.javabase.y2022.m05.day0516.v05;

import java.util.Stack;

/**
 * @descriptions:
 * @author: Tom 每次都需要判斷結構為空的情況
 * @date: 2022/5/16 下午 02:00
 * @version: 1.0
 */
public class Solution {
    public class MyMinStack01{
        Stack<Integer> dataStack = new Stack<>();
        Stack<Integer> minStack = new Stack<>();
        int pop(){
            if(minStack.isEmpty()){
                throw new RuntimeException("stack is empty~");
            }
            int ans = this.minStack.pop();
            this.dataStack.pop();
            return ans;
        }
        void push(int value){
            if(minStack.isEmpty()){
                this.minStack.push(value);
            } else if(value < this.getMin()){
                this.minStack.push(value);
            } else {
                this.minStack.push(this.getMin());
            }
            this.dataStack.push(value);
        }
        int getMin(){
            if(this.minStack.isEmpty()){
                throw new RuntimeException("stack is empty.");
            }
            int min = this.minStack.peek();
            return min;
        }
    }
    public class MyStack02{
        Stack<Integer> dataStack = new Stack<>();
        Stack<Integer> minStack = new Stack<>();
        int getMin(){
            return this.minStack.peek();
        }
        void push(int value){
            if(this.minStack.isEmpty()){
                this.minStack.push(value);
            } else if(value <= this.getMin()){
                this.minStack.push(value);
            }
            this.dataStack.push(value);
        }
        int pop(){
            if(this.minStack.isEmpty()){
                throw new RuntimeException("stack is empty");
            }
            int ans = this.minStack.peek();
            if(ans == this.dataStack.peek()){
                this.minStack.peek();
            }
            this.dataStack.pop();
            return ans;
        }
    }
}
